package top.infopub.mgr.bpa.web;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.BpaPublicProInfoDto;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.bpa.domain.MonthEffectPlanDomain;
import top.infopub.mgr.bpa.dto.BpaMonthEffectPlanDto;
import top.infopub.mgr.bpa.jsonVo.BpaMonthEffectPlanJsonVo;
import top.infopub.mgr.bpa.service.BpaMonthEffectPlanService;
import top.infopub.mgr.bpa.service.BpaProcessManageService;
import top.infopub.mgr.bpa.vo.BpaMonthEffectPlan;
import top.infopub.mgr.bpa.vo.EffectPlanPublicVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.mgr.hr.service.OrgInfoService;
import top.infopub.mgr.hr.service.StaffInfoService;
import top.infopub.mgr.mkt.service.ProjectInfoService;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * Created by wurz on 2021/6/20.
 */
@Controller
@RequestMapping("/monthEffectPlan")
public class BpaMonthEffectPlanController extends GenericFlowController {
    @Autowired
    private StaffInfoService staffInfoService;

    @Autowired
    private CodeGeneratorConfigService codeGeneratorConfigService;

    @Autowired
    private OrgInfoService orgInfoService;
    @Autowired
    private BpaProcessManageService bpaProcessManageService;

    @Resource
    private BpaMonthEffectPlanService bpaMonthEffectPlanService;

    @Autowired
    private ProjectInfoService projectInfoService;

    /**
     * 跳转列表页
     *
     * @return
     */
    @RequestMapping("/list")
    public String list(HttpServletRequest request) {
        return "bpa/lay-monthEffectPlan-list";
    }

    /**
     * 月度创效计划列表数据
     *
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<MonthEffectPlanDomain> queryDataByCondition(EffectPlanPublicVo effectPlanPublicVo,
                                                             ParamPage paramPage,
                                                             String monthStartDateStr,
                                                             String monthEndDateStr) {
        Page<MonthEffectPlanDomain> page = new Page<MonthEffectPlanDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = bpaMonthEffectPlanService.selectList(page,effectPlanPublicVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }

    /**
     * 根据id查询  > 注意：方法命名必须为：toDealOrDetail
     *
     * @param id 投建项目id
     * @return
     * @see
     */
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id, String proCode) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("id", id);
        request.setAttribute("isAuditFlag", "1");
        if (StringUtils.isNotBlank(id)) {
            //修改成主表数据
            BpaMonthEffectPlan detailObject = bpaMonthEffectPlanService.selectOne(id);
            request.setAttribute("auditTabFlag", "0");//登录人和审批人一致使用该字段是否显示审核信息
            if (detailObject != null){
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), detailObject.getProcessStatus())
                        || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), detailObject.getProcessStatus())) {
                    request.setAttribute("isAuditFlag", "0");
                    if (StringUtils.isNotBlank(detailObject.getProcessId()) && detailObject.getProcessId().length() > 10) {
                        request.setAttribute("processId", detailObject.getProcessId());
                        String auditTabFlag = bpaProcessManageService.checkShowAuditTab(detailObject.getProcessId(), vo);
                        if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), detailObject.getProcessStatus())
                                && StringUtils.equals("0", auditTabFlag)) {
                            request.setAttribute("isAuditFlag", "0");
                        }
                        request.setAttribute("auditTabFlag", auditTabFlag);
                    }
                }
            }
            request.setAttribute("proCode", proCode);
        }
        //统一一个详情，根据主表返回的类型显示不同的数据--前端会处理，后端只要返回标识
        return "bpa/lay-MonthlyBenefitPlan-detail";
    }

    @RequestMapping("/queryDetail")
    @ResponseBody
    public RspVo querySetupDetail(String id) {
        return RspVo.getSuccessResponse(bpaMonthEffectPlanService.queryDetailById(id));
    }

    //根据procode获取项目的基本信息
    @RequestMapping("/queryProjDetail")
    @ResponseBody
    public RspVo queryProjDetail(String proCode) {
        BpaMonthEffectPlanJsonVo jsonVo = new BpaMonthEffectPlanJsonVo();
        //项目的基础信息
        BpaPublicProInfoDto dto = projectInfoService.queryBpaProInfoByProCode(proCode);
        BpaMonthEffectPlanDto info = new BpaMonthEffectPlanDto();
        BeanUtils.copyProperties(dto, info);
        jsonVo.setInfo(info);
        return RspVo.getSuccessResponse(jsonVo);
    }

    /**
     * 新增/编辑
     * @param bodyStr
     * @param submitType
     * @return
     */
    @RequestMapping("/edit")
    public @ResponseBody RspVo edit(String bodyStr, Integer submitType) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String str = StringEscapeUtils.unescapeJava(bodyStr);
        BpaMonthEffectPlanJsonVo jsonVo = JSON.parseObject(str, BpaMonthEffectPlanJsonVo.class);
        //传入参数自己看情况
        RspVo rspVo = bpaMonthEffectPlanService.edit(jsonVo, submitType, vo);
        return rspVo;
    }
}
